package gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class GUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8142723230451333607L;
	private JTextArea textLog;
	private JTextField xPos;
	private JTextField yPos;
	private JTextField command;
	private final int INCRAMENT = 1;

	public GUI() {
		this("Camera Interface");
	}
	
	public GUI(String name){
		super(name);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(800, 600);
		this.setLayout(null);
		initPanTiltButtons();
		initSetPos();
		initModeButtons();
		initMessageBox();
		initPicture();
		setVisible(true);
	}

	private void initPanTiltButtons() {
		JPanel panTiltPanel = new JPanel(new GridLayout(3, 3));

		JButton up = new JButton("Up");
		up.setActionCommand("tilt "+INCRAMENT);
		up.addActionListener(new ButtonListener());
		
		JButton down = new JButton("Down");
		down.addActionListener(new ButtonListener());
		down.setActionCommand("tilt -"+INCRAMENT);
		
		JButton left = new JButton("Left");
		left.addActionListener(new ButtonListener());
		left.setActionCommand("pan -"+INCRAMENT);
		
		JButton right = new JButton("Right");
		right.addActionListener(new ButtonListener());
		right.setActionCommand("pan "+INCRAMENT);
		
		panTiltPanel.add(new JPanel()); // Blank
		panTiltPanel.add(up);
		panTiltPanel.add(new JPanel()); // Blank
		panTiltPanel.add(left);
		panTiltPanel.add(new JPanel()); // Blank
		panTiltPanel.add(right);
		panTiltPanel.add(new JPanel()); // Blank
		panTiltPanel.add(down);
		panTiltPanel.add(new JPanel()); // Blank

		panTiltPanel.setBackground(Color.black);
		panTiltPanel.setSize(250, 250); // At least 250 x 250

		this.add(panTiltPanel);
	}

	private void initSetPos() {
		JPanel setPos = new JPanel();
		setPos.setLayout(null);
		setPos.setSize(250, 120);
		setPos.setLocation(0, 250);

		xPos = new JTextField();
		xPos.setSize(120, 50);
		xPos.setLocation(10, 10);
		xPos.setText("0");
		
		yPos = new JTextField();
		yPos.setSize(120, 50);
		yPos.setLocation(10, 60);
		yPos.setText("0");
		
		JButton sendX = new JButton("Send X");
		sendX.addActionListener(new ButtonListener());
		sendX.setActionCommand("setx");
		sendX.setSize(100, 50);
		sendX.setLocation(140, 10);

		JButton sendY = new JButton("Send Y");
		sendY.addActionListener(new ButtonListener());
		sendY.setActionCommand("sety");
		sendY.setSize(100, 50);
		sendY.setLocation(140, 60);

		setPos.add(xPos);
		setPos.add(yPos);
		setPos.add(sendX);
		setPos.add(sendY);

		setPos.setBackground(Color.red);

		this.add(setPos);
	}

	private void initModeButtons() {
		JButton manuel = new JButton("Manuel");
		manuel.addActionListener(new ButtonListener());
		manuel.setActionCommand("manuel");
		
		JButton calibrate = new JButton("Calibrate");
		calibrate.addActionListener(new ButtonListener());
		calibrate.setActionCommand("calibrate");
		
		JButton auto = new JButton("Full-auto");
		auto.addActionListener(new ButtonListener());
		auto.setActionCommand("track");
		
		JPanel modePanel = new JPanel(new GridLayout(3, 1));
		modePanel.add(manuel);
		modePanel.add(calibrate);
		modePanel.add(auto);
		modePanel.setSize(250, 200);
		modePanel.setLocation(0, 370);

		this.add(modePanel);
	}

	private void initMessageBox() {
		JPanel messagePanel = new JPanel();
		messagePanel.setSize(300, 300);
		messagePanel.setLocation(250, 270);
		messagePanel.setBackground(Color.green);
		messagePanel.setLayout(null);

		textLog = new JTextArea();
		textLog.setLocation(0, 0);
		JScrollPane scrollText = new JScrollPane(textLog);
		scrollText.setLocation(new Point(0, 0));
		scrollText.setSize(300, 275);
		messagePanel.add(scrollText);

		command = new JTextField();
		command.setSize(200, 25);
		command.setLocation(0, 275);
		messagePanel.add(command);

		JButton send = new JButton("Send");
		send.addActionListener(new ButtonListener());
		send.setActionCommand("sendCommand");
		send.setSize(100, 25);
		send.setLocation(200, 275);
		messagePanel.add(send);

		this.add(messagePanel);
	}

	private void initPicture() {
		JPanel picture = new JPanel();
		picture.setBackground(Color.black);
		picture.setSize(300, 300);
		picture.setLocation(250, 0);

		this.add(picture);
	}

	private class ButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().equals("setx")){
				textLog.append(e.getActionCommand()+" "+xPos.getText()+"\n");
			}else if(e.getActionCommand().equals("sety")){
				textLog.append(e.getActionCommand()+" "+yPos.getText()+"\n");
			}else{
				textLog.append(e.getActionCommand()+"\n");
			}
		}
	}

}
